package com.poly.system.controller;

import com.poly.common.core.domain.R;
import com.poly.common.core.validate.Add;
import com.poly.common.core.web.controller.BaseController;
import com.poly.common.log.annotation.Log;
import com.poly.common.log.enums.BusinessType;
import com.poly.common.security.annotation.RequiresPermissions;
import com.poly.common.security.utils.SecurityUtils;
import com.poly.system.pojo.dto.SysAppMenuAuthDTO;
import com.poly.system.pojo.query.SysAppMenuAuthQuery;
import com.poly.system.pojo.vo.SysAppMenuAuthVO;
import com.poly.system.service.ISysAppMenuAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = "应用菜单授权")
@RestController
@RequestMapping("/appmenu/auth")
public class SysAppMenuAuthController extends BaseController {

    @Autowired
    private ISysAppMenuAuthService appMenuAuthService;

    @ApiOperation("查询APP能力菜单列表")
    @RequiresPermissions("app:menu:auth")
    @GetMapping("list")
    public R<List<SysAppMenuAuthVO>> list(SysAppMenuAuthQuery query){
        return R.ok(appMenuAuthService.list(query));
    }

    @ApiOperation("APP能力授权")
    @Log(title = "应用菜单授权管理", businessType = BusinessType.GRANT)
    @RequiresPermissions("app:menu:auth")
    @PostMapping()
    public R auth(@RequestBody @Validated({Add.class}) SysAppMenuAuthDTO model){
        if(appMenuAuthService.auth(model, SecurityUtils.getUserId())){
            return R.ok();
        }
        return R.fail();
    }

    @ApiOperation("取消授权")
    @Log(title = "应用菜单授权管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("app:menu:auth:cancel")
    @DeleteMapping("/{ids}")
    public R cancelAuth(@PathVariable Long[] ids){
        if(appMenuAuthService.cancelAuth(Arrays.asList(ids))){
            return R.ok();
        }
        return R.fail();
    }
}
